package org.bingdianleil.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.bingdianleil.config.DataSourceConfig;
import org.bingdianleil.util.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StockShowDao {

	@Autowired
	private DataSourceConfig dataSourceConfig;
	
	/**
	 * 获取股票的10分钟线
	 * @param stockId
	 * @param dataDate
	 * @return
	 */
	public List<List<String>> getStockList(String stockId,String dataDate) {
		List<List<String>> retList = new ArrayList<List<String>>();
		Connection connection = null;
		PreparedStatement pStatement = null;
		ResultSet rs =null;
		String sql="SELECT   t.stock_name,t.price,t.data_time FROM    dat_stock_history t where t.stock_id = ? and t.data_date = ? order by t.data_time asc";
		try {
			connection = dataSourceConfig.druidDataSource().getConnection();
			pStatement = connection.prepareStatement(sql);
			pStatement.setString(1, stockId);
			pStatement.setString(2, dataDate);
			rs = pStatement.executeQuery();
			while(rs.next()) {
			   List<String> row = new ArrayList<String>();
			   row.add(rs.getString(1));
			   row.add(rs.getString(2));
			   row.add(rs.getString(3));
			   retList.add(row);
			}
		} catch (SQLException e) {
			 
			e.printStackTrace();
		}finally {
			DataSourceUtil.Release(rs,pStatement,connection);
		}
		
		return retList;
	}
	
	/**
	 * 获取股票的日线
	 * @param stockId
	 * @param bgDate
	 * @param endDate
	 * @return
	 */
	public List<List<String>> getStockList(String stockId,String bgDate,String endDate){
		List<List<String>> retList = new ArrayList<List<String>>();
		StringBuilder sqlBuilder= new StringBuilder("SELECT  \n");
		sqlBuilder.append("    t.stock_name, \n" ); 
		sqlBuilder.append("    t.today_bg_price, \n" ); 
		sqlBuilder.append("    t.price, \n" ); 
		sqlBuilder.append("    t.today_top_price, \n" ); 
		sqlBuilder.append("    t.today_lower_price, \n" ); 
		sqlBuilder.append("    t.data_date \n" ); 
		sqlBuilder.append("FROM \n" ); 
		sqlBuilder.append("    stockdb.dat_stock_history t \n" ); 
		sqlBuilder.append("WHERE \n" ); 
		sqlBuilder.append("    t.stock_id = ?  \n" ); 
		sqlBuilder.append("        AND t.data_date BETWEEN ? AND ? \n" ); 
		sqlBuilder.append("ORDER BY t.data_date ASC ");
		Connection connection = null;
		PreparedStatement pStatement = null;
		ResultSet rs =null;
		
		try {
			connection = dataSourceConfig.druidDataSource().getConnection();
			pStatement = connection.prepareStatement(sqlBuilder.toString());
			pStatement.setString(1, stockId);
			pStatement.setString(2, bgDate);
			pStatement.setString(3, endDate);
			rs = pStatement.executeQuery();
			while(rs.next()) {
				   List<String> row = new ArrayList<String>();
				   row.add(rs.getString(1));
				   row.add(rs.getString(2));
				   row.add(rs.getString(3));
				   row.add(rs.getString(4));
				   row.add(rs.getString(5));
				   row.add(rs.getString(6));
				   retList.add(row);
			}
			
		 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DataSourceUtil.Release(rs,pStatement,connection);
		}
		
		
		
		return retList;
	}
	
	 
	
}
